#pragma once

#include "public_define.h"
#include "fileReupload.h"
#include "data_structure.h"
#include "file_task.h"
#include "eventcommand.h"
#include "mutex"
using namespace std;
namespace DcAdr
{
    typedef enum FILEREUPLOAD_STATUS
    {
        IDLE = 0,       //空闲
        REALWORK = 1,    //文件上传中
    } FILEREUPLOAD_STATUS;

    typedef struct Reupload_Que_Info
    {
        string fileName;
        string filePath;
        string taskId;
        string eventCode;
        std::string businessType;//文件上传的业务类型
        int reuploadTimes;
    } Reupload_Que_Info;

    class FileReupload : public EventCommand
    {
    public:
        static FileReupload &getInstance();
        void dealUploadResult(std::string filePath, uint32_t result);
        void timerReupload();

        // 文件信息写入配置文件
        //void writeFileInfo(const string &filePath, const string &taskId, const int level, const string &eventCode);
        // 文件信息写入配置文件,增加businessType
        void writeFileInfo(const string &filePath, const string &taskId, const int level, const string &eventCode,const string &businessType);
        // 写入文件handle
        void writeFileHandle(const string &filePath, const string &fileHandle);
        // 写入分片文件MD5
        void writeChunkFileMd5(const string &filePath, const string &chunkFileMd5);
        // 写入分片文件查询合并结果次数
        void writeQueryTimes(const string &filePath);
        // 分片文件查询合并结果次数置0
        void writeQueryTimesToZero(const string &filePath);
        // 写入chunkOperation参数为true
        void writeChunkOperation(const string &filePath);
    
        // 读取优先级最高的一个文件
        bool readOneFileTask(Reupload_Que_Info &retTask);
        // 读取指定文件的json配置
        bool readOneFileJson(const string &filePath, Json::Value &oneFile);

    private:
        FileReupload(/* args */);
        ~FileReupload();
        
        // 设置文件重传状态
        void setReuploadStatus(FILEREUPLOAD_STATUS status);

        // 读取重传配置文件Json
        bool readReuploadFileJson(string &jsonString, Json::Value &root);

        // 重传配置文件锁
        mutex mutex_writeReuploadFile;
        // 最大重传次数
        const int reuploadTime = 3;

        FILEREUPLOAD_STATUS m_reuploadSts{FILEREUPLOAD_STATUS::IDLE};
    };
}
